
$(document).ready(function () {			
 		var arr = area();
		for(var i=0; i<arr.length;i++){
			$("#change").append("<option value="+arr[i].areaCode+">"+arr[i].province+" "+arr[i].city+" "+arr[i].area+"</option>");
		}
		var arealist=$("#change option:selected").text();
		var list = arealist.split(" ");
		var areaName=list[2];
		changeOne();
		changeTwo();
		loadData(areaName);
		window.setInterval("timeEvent()",30000);		
});

function loadData(areaName){
	var firstlng="";
    var firstlat="";
	//初始化地图对象，加载地图
   	var map = new AMap.Map("container", {resizeEnable: true});
	AMap.plugin(['AMap.ToolBar','AMap.Geocoder'],
    function(){
        map.addControl(new AMap.ToolBar());

        map.addControl(new AMap.Geocoder());

	});

		var areaCode = $("#change").val();
		var userType = $("#userType").val();
		if(userType==1){
			var da='';
			$.ajax({
		            url: urlcore + "/api/address/addressShop?areaCode="+areaCode,
		            type: "get",
		            dataType: 'json',
		            contentType: "application/json;charset=utf-8",
		            async: false,
		            success:function(data){
		                if (data.success == true) {
		                    da = data.data;                   
		                } else if (data.code == 'OVERTIME'){
		                    var thisUrl = window.location.href;
		                    if (thisUrl.indexOf('login.html') <= -1) {
		                        top.window.location.href="login.html";
		                    }
		                } else {
		                    alert(data.msg);
		                }
		            },
		            error:function() {
		                /* Act on the event */
		                alert("error");
		            }
		    });
	
	    var lnglats = new Array();
		for(var a = 0; a < da.length; a++){
			lnglats[a] = [da[a].lng , da[a].lat];
		}
	    var infoWindow = new AMap.InfoWindow({offset: new AMap.Pixel(0, -30)});
	    //显示标记
	    for (var i = 0, marker; i < lnglats.length; i++) {
	        var marker = new AMap.Marker({
	            position: lnglats[i],
	            map: map,
	            icon: new AMap.Icon({            
	            	image: "img/shanghu@2x.png",
	        	})   
	            
	        });
	        
	        //给标注内容赋值
			marker.setLabel({//label默认蓝框白底左上角显示，样式className为：amap-marker-label
			    offset: new AMap.Pixel(0,-20),//修改label相对于maker的位置
			    content: da[i].shopname
			});
	        //marker.emit('click', {target: marker});
	    }
	    map.setFitView();
		}
		if(userType==2){
			var da='';
			$.ajax({
		            url: urlcore + "/api/dynamicPlace/riderDynamicPlace?areaCode="+areaCode,
		            type: "get",
		            dataType: 'json',
		            contentType: "application/json;charset=utf-8",
		            async: false,
		            success:function(data){
		                if (data.success == true) {
		                    da = data.data;  
		                } else if (data.code == 'OVERTIME'){
		                    var thisUrl = window.location.href;
		                    if (thisUrl.indexOf('login.html') <= -1) {
		                        top.window.location.href="login.html";
		                    }
		                } else {
		                    alert(data.msg);
		                }
		            },
		            error:function() {
		                /* Act on the event */
		                alert("error");
		            }
		            
		    });	
		var zooms=$("#zoom").html();
		var centers=$("#center").text();
		var arr=new Array();
		var lng="";
		var lat="";
		arr=centers.toString().split(",");
		lng=arr[0];
	    lat=arr[1];
	 
	    if(centers==""){    	
	    	map.setCity(areaName);
	    }else{
	    	map.setZoom(Number(zooms));
	    	map.setCenter([lng, lat]);
	    }
	       
	    var lnglats = new Array(); 
		for(var a = 0; a < da.length; a++){
			lnglats[a] = [da[a].lng , da[a].lat];
		}
	    var infoWindow = new AMap.InfoWindow({offset: new AMap.Pixel(0, -30)});
	    //显示标记
	    for (var i = 0, marker; i < lnglats.length; i++) {
	        var marker = new AMap.Marker({
	            position: lnglats[i],
	            map: map,
	            icon: new AMap.Icon({            
	            	image: "img/qishou@2x.png",
	        	}) 
	            
	        });
	        //给标注内容赋值
			marker.setLabel({//label默认蓝框白底左上角显示，样式className为：amap-marker-label
			    offset: new AMap.Pixel(0, -10),//修改label相对于maker的位置
			    content: da[i].rideName+" "+da[i].num+"单"
			});
			//将骑手id存储
	        marker.setTitle(da[i].rideId+"");
	        //alert(marker.getTitle());
	        marker.on('click', markerClick);
	        //marker.emit('click', {target: marker});
	    }

	    function markerClick(e) {
	    	$('#today').css('display','block');
	    	$("#thisId_update").val(e.target.getTitle());
	        infoWindow.setContent(e.target.getLabel().content);
	        infoWindow.open(map, e.target.getPosition());
	    }
	    //map.setFitView();
		}
	   	//监听拿到当前的中心点和缩放级别
	    AMap.event.addListener(map,'zoomend',function(){
	    	 $("#zoom").html(map.getZoom());
		});
		AMap.event.addListener(map,'dragend',function(){
	    	 $("#center").text(map.getCenter()); 
		});
	

	

}


//今日情况
function today(){
	var id = $("#thisId_update").val();
		$.ajax({
		url: urlcore + "/api/user/getRideOneDayUseByMap?rideId="+id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				$('#todayMoney').val(data.data.currentDayMoney);
				$('#todayOrder').val(data.data.currentDayOrderNum);
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}
			} else {
				alert(data.msg);
			}
		},
		error:function() {
			/* Act on the event */
			alert("error");
		}
	});
}

//当前订单
function currentOrder(){
	var id = $("#thisId_update").val();
		$.ajax({
		url: urlcore + "/api/user/riderCurrentOrders?rideId="+id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				var da=data.data;
				//初始化
				$("#orderName").empty();
				$("#orderName").val("");
				$('#addressDetail').val("");
				$('#riderName').val("");
				$('#riderPhone').val("");
				if(JSON.stringify(da)!="[]"){
					//给下拉框赋值
					for(var i=0; i<da.length;i++){
						$("#orderName").append("<option value="+da[i].id+">"+da[i].orderNumber+"</option>");
					}
					//拿到订单id获取详细信息
					orderDetail();
					
				 	$("#orderName").change(function () {
						orderDetail();
		   		    });
			}	
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}
			} else {
				alert(data.msg);
			}
		},
		error:function() {
			/* Act on the event */
			alert("error");
		}
	});
}

//当前订单详细信息
function  orderDetail(){
	var orderId = $("#orderName").find("option:selected").val();
	var order = findOrder(orderId);
	$('#addressDetail').val(order.addressUser.addressDetail);
	$('#riderName').val(order.ride.realName);
	$('#riderPhone').val(order.ride.phone);
}

function findOrder(orderId){
	var da="";
	$.ajax({
		url: urlcore + "/api/order/orderDetils?id="+orderId,
		type: "post",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		async: false,
		success:function(data){
			if (data.success == true) {
				da=data.data;
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}
			} else {
				alert(data.msg);
			}
		},
		error:function() {
			/* Act on the event */
			alert("error");
		}	
	});
	return da;
}


//指派订单
function sendOrder(){
	var id = $("#thisId_update").val();
		$.ajax({
		url: urlcore + "/api/order/allSendOrders",
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		async: false,
		success:function(data){
			if (data.success == true) {
				var da=data.data;
				//初始化
				$("#orderNameTwo").empty();
				$("#orderNameTwo").val("");
				$('#addressDetailTwo').val("");
				$('#UserNameTwo').val("");
				$('#UserPhoneTwo').val("");
				if(JSON.stringify(da)!="[]"){
					//给下拉框赋值
					for(var i=0; i<da.length;i++){
						$("#orderNameTwo").append("<option value="+da[i].id+">"+da[i].orderNumber+"</option>");
					}
					//拿到订单id获取详细信息
					orderDetailTwo();					
				 	$("#orderNameTwo").change(function () {
						orderDetailTwo();
		   		   });		   		    
			}	
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}
			} else {
				alert(data.msg);
			}
		},
		error:function() {
			/* Act on the event */
			alert("error");
		}
	});
}

//指派订单详细信息
function  orderDetailTwo(){
	var orderId = $("#orderNameTwo").find("option:selected").val();
	var order = findOrder(orderId);
	$('#addressDetailTwo').val(order.addressUser.addressDetail);
	$('#UserNameTwo').val(order.shop.realName);
	$('#UserPhoneTwo').val(order.shop.phone);
}

//指派骑手接单
function  sendRider(){
	var orderId = $("#orderNameTwo").find("option:selected").val();
	var id = $("#thisId_update").val();
	$.ajax({
		url: urlcore + "/api/user/sendOrderToRider?orderId="+orderId+"&rideId="+id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		async: false,
		success:function(data){
			if (data.success == true) {
				alert("指派成功");
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}
			} else {
				alert(data.msg);
			}
		},
		error:function() {
			/* Act on the event */
			alert("error");
		}	
	});
}

function area(){
	var da='';
	$.ajax({
            url: urlcore + "/api/openArea/allArea",
            type: "get",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            async: false,
            success:function(data){
                if (data.success == true) {
                    da = data.data;                   
	                /* $('#messUrl').html(imgStr2);                  
                    $('#title').text(da.title);
                    $('#content').html(da.content);*/
                } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
        return da;
}

/**
 * 下拉框查询
 */
function changeOne(){
       $("#change").change(function () {
	       	var arealist=$("#change option:selected").text();
			var list = arealist.split(" ");
			var areaName=list[2];
       		loadData(areaName);      		
       		$('#today').css('display','none');
   		});
}
/**
 * 下拉框查询2
 */
function changeTwo(){
       $("#userType").change(function () {
	       	var arealist=$("#change option:selected").text();
			var list = arealist.split(" ");
			var areaName=list[2];
       		loadData(areaName);
       		$('#today').css('display','none');
   		});
}

function timeEvent(){
       	var arealist=$("#change option:selected").text();
		var list = arealist.split(" ");
		var areaName=list[2];
   		loadData(areaName);
}



